﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Media.Animation;
using System.Threading;
using System.Windows.Threading;

namespace StatusPlan3
{
    /// <summary>
    /// Interaction logic for Window_Loading.xaml
    /// </summary>
    public partial class Window_Loading : Window
    {
        Storyboard Showboard;
        Storyboard Hideboard;
        public delegate void ShowDelegate(string txt);
        public delegate void HideDelegate();
        ShowDelegate showDelegate;
        HideDelegate hideDelegate;
        MainWindow main;

        public Window_Loading()
        {
            InitializeComponent();
            Assembly assembly = Assembly.GetExecutingAssembly();
       
            showDelegate = new ShowDelegate(this.showText);
            hideDelegate = new HideDelegate(this.hideText);
            Showboard = this.Resources["showStoryBoard"] as Storyboard;
            Hideboard = this.Resources["HideStoryBoard"] as Storyboard;

            VersionTextBlock.Text = "Útgáva " + assembly.GetName().Version.Major + "." + assembly.GetName().Version.Minor + "." + assembly.GetName().Version.Build + "." +  assembly.GetName().Version.Revision;
        }

        public bool? showManualLogin()
        {
            loadBar.Visibility = System.Windows.Visibility.Collapsed;
            txtLoading.Visibility = System.Windows.Visibility.Collapsed;
            LoginBox.Visibility = System.Windows.Visibility.Visible;
            UserTextBox.Focus();
            bool? result = this.ShowDialog();
            return result;
        }

        public void setMainWindow(MainWindow main)
        {
            this.main = main;
        }

        private void load()
        {
            Thread.Sleep(2000);
            this.Dispatcher.Invoke(showDelegate, "first data to loading");
            Thread.Sleep(2000);
            //load data 
            this.Dispatcher.Invoke(hideDelegate);

            Thread.Sleep(2000);
            this.Dispatcher.Invoke(showDelegate, "second data loading");
            Thread.Sleep(2000);
            //load data
            this.Dispatcher.Invoke(hideDelegate);


            Thread.Sleep(2000);
            this.Dispatcher.Invoke(showDelegate, "last data loading");
            Thread.Sleep(2000);
            //load data 
            this.Dispatcher.Invoke(hideDelegate);

            //close the window
            Thread.Sleep(2000);
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate() { Close(); });
           
        }

        public void showText(string txt)
        {
            if (this.Dispatcher.Thread == Thread.CurrentThread)
            {
                txtLoading.Text = txt;
                BeginStoryboard(Showboard);
            }
            else
            {
                this.Dispatcher.Invoke(showDelegate, txt);
            }
        }

        public void hideText()
        {
            if (this.Dispatcher.Thread == Thread.CurrentThread)
            {
                BeginStoryboard(Hideboard);
            }
            else
            {
                this.Dispatcher.Invoke(hideDelegate);
            }

        }

        private void UserTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                PasswordTextBox.Focus();
        }

        private void PasswordTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                loginButton.Focus();
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            if (UserTextBox.Text.Trim().Length > 0 || WindowLogin.IsChecked != null && WindowLogin.IsChecked.Value == true)
            {
                Boolean winAuth = false;
                if(WindowLogin.IsChecked != null)
                    winAuth = WindowLogin.IsChecked.Value;

                Boolean b = main.login(UserTextBox.Text, PasswordTextBox.Password, winAuth);
                if (!b)
                {
                    MessageBox.Show("Brúkari ella kotan er skeiv", "Feilur: Innrita", MessageBoxButton.OK, MessageBoxImage.Error);
                    PasswordTextBox.Password = "";
                    UserTextBox.Focus();
                    UserTextBox.SelectAll();
                }
                else
                {
                    this.DialogResult = true;
                    this.Close();
                }
            }
            else
            {
                UserTextBox.Focus();
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;

            this.Close();
        }

        private void UserTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void PasswordTextBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            
        }

        private void setupButton_Click(object sender, RoutedEventArgs e)
        {

            Window_Setup setup = new Window_Setup();
            setup.Show();
        }

        

    }
}
